package com.czk.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 实现自动填充的拦截器
 *
 * @Author:ChenZhangKun
 * @Date: 2021/11/1 23:10
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    /**
     * mp执行时直接插入
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("【自动填充{}触发了】","createTime");
        this.setFieldValByName("createTime", new Date(), metaObject);
        // 插入时设置版本默认值
        this.setFieldValByName("version",1,metaObject);
        // 设置插入初始值，插入时 0代表不删除，1代表删除，逻辑删除时会框架将其设置为1
        this.setFieldValByName("deleted",0,metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("【自动填充{}触发了】","updateTime");
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}
